`

Except for the shebang line, every line that starts with a pound

sign is considered a comment. If you wrote the shebang line twice,

bash would consider the second one to be a comment.

To write a multiline comment, precede each individual line with

the pound sign, as shown in Listing 1-7.

#!/bin/bash

# This is my first script!

# Bash scripting is fun...

Listing 1-7

A multiline comment

In addition to documenting a script’s logic, comments can

provide additional metadata, such as who the author is, the scripts

version, whom to contact for issues, and more. These comments

usually appear at the top part of the script, below the shebang line.

Commands

Scripts can be as short as two lines: the shebang line and a Linux

command. Let’s write a very simple script that prints Hello World to

the terminal. Open your text editor and enter the following:

#!/bin/bash

echo "Hello World!"

In this example, we use the shebang statement to specify the

interpreter of choice, bash. Then, we use the echo command to print

the string Hello World! to the screen.

Execution

To run the script, save the file as helloworld.sh, open the

terminal, and navigate to the directory where the script resides. If

you saved the file in your home directory, you run the following set

of commands:

$ cd ~

$ chmod u+x helloworld.sh

$ ./helloworld.sh

Hello World!

We use the cd command to change directories. The tilde (~)

represents the home directory of the current running user. Next, we

set the executable (u+x) permissions using the chmod command for

Black Hat Bash (Early Access) © 2023 by Dolev Farhi and Nick Aleks